package com.cloudsigns.factorymanager2.engineering.model;

import android.text.TextUtils;
import android.util.Log;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.AccountInfoBean;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

/**
 * Created by tony on 2018/1/14.
 */

public class LoginModel1 implements ILoginModel1 {

    @Override
    public void login(final String username, final String password, final OnLoginListener loginListener) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    HttpClient client = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(ProjectUtil.LOGIN);
                    HttpResponse response;
                    // 添加http头信息
                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("Phone", username);
                    jsonObject.put("Password", password);

                    StringEntity se = new StringEntity(jsonObject.toString(), HTTP.UTF_8);
                    httpPost.setEntity(se);

                    Log.e("request URL=", ProjectUtil.LOGIN);
                    Log.e("jsonObject=", jsonObject.toString());

                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    Log.e("request=", result);
                    JSONObject json = new JSONObject(result);

                    String code = json.getString("ErrCode");
                    String errMsg = json.getString("ErrMsg");
                    if (null != loginListener) {
                        //"token":"a2b2378f-210c-45a7-be5f-95a958ade900"
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD1)) {
                            JSONObject accountJson = json.getJSONObject("Data");
                            AccountInfoBean accountInfoBean = new AccountInfoBean();
                            accountInfoBean.setAccountName(accountJson.getString("UserName"));
                            accountInfoBean.setEmail(accountJson.getString("Email"));
                            accountInfoBean.setPhone(accountJson.getString("Phone"));
                            accountInfoBean.setStatus(accountJson.getString("Status"));
                            accountInfoBean.setCNStatus(accountJson.getString("CNStatus"));
                            MyApplicaton.getApplication().setToken(accountJson.getString("Token"));
//                            ExpireTime
                            accountInfoBean.setCompanyId(accountJson.getString("CompanyId"));
                            accountInfoBean.setCompanyName(accountJson.getString("CompanyName"));
                            accountInfoBean.setRoleId(accountJson.getString("RoleId"));
                            accountInfoBean.setRoleTitle(accountJson.getString("RoleTitle"));

                            String roleCategory = accountJson.getString("RoleName");
                            if (!TextUtils.isEmpty(roleCategory)) {
                                if (roleCategory.equals("SysManager")) {
                                    accountInfoBean.setRoleName(CommonEnum.RoleName.SysManager);
                                }
                            }
                            MyApplicaton.getApplication().setAccountInfoBean(accountInfoBean);
                            loginListener.loginSuccess();
                        } else {

                            String error_str = code;
//                            error_str += "," + ConstantUtil.getErrorCode(code);
//                            if (!json.isNull("error_str")) {
//                                error_str += "," + json.getString("error_str");
//                            }
                            error_str += ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += json.getString("error_str");
                            }
                            loginListener.loginFailed(error_str);
                        }

                    }
                    String TotalCount = json.getString("TotalCount");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnLoginListener {
        void loginSuccess();

        void loginFailed(String desc);
    }
}
